#include "GameDemo.h"

#ifdef _DEBUG
#pragma comment( lib, "../Debug/GameEngine.lib" )
#else
#pragma comment( lib, "../Release/GameEngine.lib" )
#endif

#include <tchar.h>

GameDemo::GameDemo( void )
{
    // create a new graphics device manager
    mGraphicsDeviceManager = new GraphicsDeviceManager( *this );
}

GameDemo::~GameDemo( void )
{
    if( mGraphicsDeviceManager != NULL )
    {
        delete mGraphicsDeviceManager;
    }
}

void GameDemo::Initialize( void )
{
    Game::Initialize();
    
    // set up graphics preferences
    mGraphicsDeviceManager->SetFullScreen( FALSE );
    mGraphicsDeviceManager->SetPreferMultiSampling( TRUE );
    mGraphicsDeviceManager->SetPreferredBackBufferFormat( SURFACE_FORMAT_COLOR );
    mGraphicsDeviceManager->SetPreferredBackBufferHeight( 600 );
    mGraphicsDeviceManager->SetPreferredBackBufferWidth( 800 );
    mGraphicsDeviceManager->SetPreferredDepthStencilFormat( DEPTH_FORMAT_DEPTH16 );
    mGraphicsDeviceManager->SetSynchronizeWithVerticalRetrace( TRUE );
    mGraphicsDeviceManager->ApplyChanges();

    // set up projection matrix
    float32 field_of_view = 3.14f / 4.0f;
    float32 aspect_ratio = GetGraphicsDevice().GetViewport().AspectRatio;
    float32 near_plane = 1.0f;
    float32 far_plane = 100.0f;
    Matrix::CreatePerspectiveFieldOfView( field_of_view, aspect_ratio, near_plane, far_plane, mProjectionMatrix );
}

void GameDemo::LoadContent( void )
{
    Game::LoadContent();
}

void GameDemo::BeginRun( void )
{
    Game::BeginRun();

    // save the current window title
    mGameName = GetGameWindow().GetTitle();
}

void GameDemo::Update( const GameTime& game_time )
{
    Game::Update( game_time );

    // add fps to window title
    static tchar buffer[ 100 ];
    swprintf( buffer, sizeof( buffer ), L" - FPS : %.2f", 1.0 / game_time.GetElapsedTime().GetTotalSeconds() );
    GetGameWindow().SetTitle( mGameName + buffer );

    // update view matrix
    Vector3 camera( 10.0f, 10.0f, 10.0f );
    Vector3 target( 0.0f, 0.0f, 0.0f );
    Vector3 up( 0.0f, 1.0f, 0.0f );
    Matrix::CreateLookAt( camera, target, up, mViewMatrix );
}

void GameDemo::Draw( const GameTime& game_time )
{
    Game::Draw( game_time );
}

void GameDemo::EndRun( void )
{
    Game::EndRun();
}

void GameDemo::UnloadContent( void )
{
    Game::UnloadContent();
}
